﻿using EyFramework.Common;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EyFramework.Library
{
    public partial class FrmMainBase : DevExpress.XtraBars.ToolbarForm.ToolbarForm, IFormBase
    {
        public DevExpress.LookAndFeel.DefaultLookAndFeel DefaultLookAndFeel;

        public FrmMainBase()
        {
            InitializeComponent();
            this.DefaultLookAndFeel = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            this.LoadSkin();
        }

        public void LoadSkin()
        {
            if (Globals.appConfigure != null)
                SetSkin(Globals.GetSysConfig(EyFramework.Common.Constants.CFG_SkinName));
        }

        public void SetSkin(string skinName)
        {
            this.DefaultLookAndFeel.LookAndFeel.SkinName = skinName;
        }

        /// <summary>
        /// 异步线程方法，异步使用多线程的方式调用函数，
        /// 注意，此方法的使用条件，异步调用的函数内部不能做操作主线程的事情，比如更新界面UI。如果确实需要，请使用invoke 委托的方式更新，
        /// 而不能直接更新
        /// </summary>
        /// <param name="action"></param>
        /// <param name="showLoading"></param>
        /// <returns></returns>
        public object DoTaskAsync(Func<object> action, bool showLoading = false)
        {
            if (showLoading)
            {
                ShowLoading("异步处理中，请耐心等待...");
            }
            try
            {
                Task<object> task = new Task<object>(action);
                task.Start();
                task.Wait();
                if (showLoading)
                {
                    CloseLoading();
                }
                Application.DoEvents();
                return task.Result;
            }
            catch (Exception ex)
            {
                CloseLoading();
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }
                else
                {
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 异步任务，不关心结果，调用后，继续主线程的操作。
        /// </summary>
        /// <param name="action"></param>
        /// <param name="showLoading"></param>
        public void DoActionAsync(Action action)
        {
            Task task = new Task(action);
            task.Start();
        }

        public void ShowLoading(string loadingText = "")
        {
            if (!loading.IsSplashFormVisible)
            {
                loading.ShowWaitForm();
                if (!string.IsNullOrEmpty(loadingText))
                {
                    loading.SetWaitFormDescription(loadingText);
                }
            }
        }

        public void CloseLoading()
        {
            if (loading.IsSplashFormVisible)
            {
                loading.CloseWaitForm();
            }
        }
    }
}